var API = API || {};
var log = console.log;

API.x_excCalculation = {
    CalculationManager: function(x_formForm, x_formSelect, x_formMask, resultsHTML, language) {

        /**
         * @param form        form
         * @param select      persons drop down elements
         */

        var parent = this;
        var form = x_formForm;
        var select = x_formSelect;
        var resultsHTML = resultsHTML;

        this.Data = {
            available: false,
            message: ''
        };

        this.Calculation = {
            Get: function() {

                $(x_formMask).mask(language['calculating'] + "...");

                var serialize = $(form).serialize();

                $.ajax({
                    url: API.Data.lang_url + '/excursions/availability?' + serialize,
                    method: 'get',
                    success: parent.Calculation.Complete
                });

            },
            Complete: function(response) {

                response = $.parseJSON(response);
//                console.log(response);

                parent.Data.available = response.isAvailiable;
                parent.Data.message = response.message;

                var persons = language['dtl_adults'] + ': ' + $(resultsHTML.nr_adults).val() + ', ' + language['dtl_children'] + ': ' + $(resultsHTML.nr_childrens).val();

                $(resultsHTML.service_persons).text(persons);
                $(resultsHTML.service_periods).text(response.periods);
                $(resultsHTML.service_total).text(response.service_total);
                $(resultsHTML.total).text(response.total_price);
                $(resultsHTML.too_many).text(response.message);

                $(x_formMask).unmask();

            }
        };

        this.Initialize = function() {

            $(select).on('change', this.Calculation.Get);
            this.Calculation.Get();
        };

    }

};